<html>
<head><meta charset="utf-8"><title>nested closure · t-compiler/wg-rfc-2229 · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/index.html">t-compiler/wg-rfc-2229</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/nested.20closure.html">nested closure</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="217612315"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/nested%20closure/near/217612315" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/nested.20closure.html#217612315">(Nov 23 2020 at 10:41)</a>:</h4>
<p>In case of nested closures we see <a href="https://github.com/sexxi-goose/rust/blob/c643dd2ec8fed2852f5eee8f776d657293a6a8f2/compiler/rustc_mir_build/src/build/expr/as_rvalue.rs#L415-L418">this assertion</a> fail</p>
<p>I think the problem here is that this <a href="https://github.com/sexxi-goose/rust/blob/c643dd2ec8fed2852f5eee8f776d657293a6a8f2/compiler/rustc_mir_build/src/build/expr/as_rvalue.rs#L386-L443">code</a><br>
expexts Field projections to be only in case of nested closures</p>
<ul>
<li>
<p>When a closure is encountered we would see VarRef with  projections= [] (in ByValue) or VarRef with projections = [Deref] (ByRef). We have VarRef here because for the parent body (assume function) these are local variables that are being captured. In this case we don't access <code>upvars_mutbs</code></p>
</li>
<li>
<p>When a nested closure is encountered we have UpvarRef and projections are [..., Field] or [..., Field, Deref] and it makes sense that we <a href="https://github.com/sexxi-goose/rust/blob/c643dd2ec8fed2852f5eee8f776d657293a6a8f2/compiler/rustc_mir_build/src/build/expr/as_rvalue.rs#L406-L412">expect</a> the base to be Local(1) because the parent is a closure. And then we know the field index &lt;= upvar_mutbls.len()</p>
</li>
</ul>



<a name="217612350"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/nested%20closure/near/217612350" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/nested.20closure.html#217612350">(Nov 23 2020 at 10:41)</a>:</h4>
<p>Though I'm kind of wondering why anything is working</p>



<a name="217721867"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/nested%20closure/near/217721867" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/nested.20closure.html#217721867">(Nov 24 2020 at 07:41)</a>:</h4>
<p><span class="user-mention silent" data-user-id="281950">Aman Arora</span> <a href="#narrow/stream/189812-t-compiler.2Fwg-rfc-2229/topic/nested.20closure/near/217612350">said</a>:</p>
<blockquote>
<p>Though I'm kind of wondering why anything is working</p>
</blockquote>
<p>Okay yeah so all of the working test cases don't mutate the capture, so that's why things looked like were working</p>



<a name="217724075"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/nested%20closure/near/217724075" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/nested.20closure.html#217724075">(Nov 24 2020 at 08:18)</a>:</h4>
<p>Okay so the mutability issue wasn't that bad to solve</p>



<a name="217727475"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/nested%20closure/near/217727475" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/nested.20closure.html#217727475">(Nov 24 2020 at 09:04)</a>:</h4>
<p>Nested will also need to have another step, since the borrow checker relies on the fact that  Field projections imply a nested closure</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>